﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace JQueryWebPart.JQueryWebPart
{
    [ToolboxItem(false)]
    public partial class JQueryWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {

        const string INIT_SCRIPT = "<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\" charset=\"utf-8\"></script> \n\n" +
            "<script type=\"text/javascript\" >\n\n" +
            "$(document).ready( function() {\n" +
              "  alert( \"JQuery Enabled\" ); \n" +
              "});\n\n" +
              "</script>";


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
            //WebPartManager mgr = this.WebPartManager;
            //mgr.DisplayModeChanged += new WebPartDisplayModeEventHandler(WebPartManager_DisplayModeChanged);

        }

        protected void Page_Load(object sender, EventArgs e)
        {            

            WebPartManager mgr = this.WebPartManager; //WebPartManager.GetCurrentWebPartManager(this.Page);

            panDesign.Visible = false;
            panRun.Visible = false;

            //litDebug.Text = "Display mode: " + mgr.DisplayMode.Name;


            if (Script == "" || Script == null )
            {
                Script = INIT_SCRIPT;
                this.SetPersonalizationDirty();  
            }

            if ((mgr.DisplayMode == WebPartManager.DesignDisplayMode || mgr.DisplayMode == WebPartManager.EditDisplayMode)) // && ShowScriptPane)
            {
                panDesign.Visible = true;
            }

            if (RunScript || mgr.DisplayMode == WebPartManager.BrowseDisplayMode)
            {                
                panRun.Visible = true;
            }

        }

        protected override void  OnPreRender(EventArgs e)
        {
            txtScript.Text = Script;
            litScript.Text = Script;

            chkRunScript.Checked = RunScript;

 	        base.OnPreRender(e);
        }

        protected void WebPartManager_DisplayModeChanged(object sender, WebPartDisplayModeEventArgs e)
        {
            WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(this.Page);
            litDebug.Text += " Display mode changed! " + mgr.DisplayMode.Name;
        }


        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            Script = txtScript.Text;
            litScript.Text = Script;
            this.SetPersonalizationDirty();     

        }

        protected void chkRunScript_OnCheckedChanged(object sender, EventArgs e)
        {
            if (chkRunScript.Checked)
            {
                litScript.Text = Script;                
            }
            panRun.Visible = chkRunScript.Checked;
            RunScript = chkRunScript.Checked;
            this.SetPersonalizationDirty();   
        }


        [WebBrowsable(false)]
        [Category("Custom Properties")]
        [WebDisplayName("Run Script")]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue("true")]
        public bool RunScript { get; set; }

        [WebBrowsable(false)]
        [Category("Custom Properties")]
        [WebDisplayName("Script")]
        [Personalizable(PersonalizationScope.Shared)]
        [DefaultValue("")]
        public string Script { get; set; }
    }
}


/*


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" charset="utf-8"></script> 

<script type="text/javascript" >

$(document).ready( function() {

  //debugger;
  alert( "1" );

  //$('table[summary="111 "]').css("table-layout", "fixed");

  //$('table[summary="111 "] tbody tr:first-child th:nth-child(1)').css("width","100px");
  //$('div[DisplayName="Name1"]').parent().css("width","100px");


});


</script>

*/